// 定义类绘制player与enemy
class sprite{
    constructor({position ,imagesrc,width,height,scale=1,framesmax=1}){
        this.position=position;
        
        this.height=height;
        this.width=width;
        this.image=new Image();//创建图片对象
        this.image.src=imagesrc;
        this.scale=scale
        this.framesmax=framesmax;
        
    }
    draw(){
        c.drawImage(this.image,this.position.x,this.position.y,this.width,this.height)
    }
    update(){
        this.draw();
        this.frameselapse++;
        
        
        if(this.frameselapse%this.frameshold===0){//每隔5帧播放一次动画
            if(this.framescurrent<this.framesmax-1){//播放动画
                this.framescurrent++;//播放动画
            }else{
                this.framescurrent=0;//循环播放动画
            }

        }
    }
    
    
}


class fighter extends sprite{
    constructor({position,velocity ,color='red',offset,imagesrc,scale=1,framesmax=1}){
        super({
            position,imagesrc,scale,framesmax
        })
        this.position=position;
        this.velocity=velocity;
        this.height=250;
        this.width=250;
        this.lastkey
        this.attackbox={
            position:{
                x:this.position.x,
                y:this.position.y//攻击碰撞盒子位置跟随角色位置
            },
            offset, //攻击碰撞盒子偏移量
                width:100,
                height:50
        }//攻击碰撞盒子
        this.color=color;
        this.isattacking
        this.health=100;

        
        this.framescurrent=0;
        this.frameselapse=0;
        this.frameshold=5;
        //this.sprites=sprites
    }
    // draw(){
    // c.fillStyle=this.color;
    //     c.fillRect(this.position.x,this.position.y,this.width,this.height);
    //     //攻击碰撞盒子显示

    //    if(this.isattacking){//攻击时显示攻击碰撞盒子
    //     c.fillStyle='blue';
    //     c.fillRect(this.attackbox.position.x,this.attackbox.position.y,this.attackbox.width,this.attackbox.height);
    //    }
    // }    


    update(){
        this.draw();
        this.attackbox.position.x=this.position.x +this.attackbox.offset.x ;//攻击碰撞盒子位置跟随角色位置
        this.attackbox.position.y=this.position.y;//攻击碰撞盒子位置跟随角色位置

        this.position.x+=this.velocity.x;//x轴移动
        this.position.y+=this.velocity.y;//y轴移动
        if(this.position.y+this.height+this.velocity.y>=canvas.height-40){
            this.velocity.y=0
        }else this.velocity.y+=gravity;
    }
    
    attack(){
        this.isattacking=true;
        setTimeout(()=>{
            this.isattacking=false;
            
        },100)//当攻击的时候只调用100毫秒，防止连续攻击
    }
}